package fluebot;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Aksel Kvitberg
 */
public class Attack {

	ArrayList<String> actions = new ArrayList<String>();
	ArrayList<String> descriptions = new ArrayList<String>();
	ArrayList<String> items = new ArrayList<String>();
	Random random = new Random();

	public Attack() {
		actions = read("Attacks/actions.txt");
		descriptions = read("Attacks/descriptions.txt");
		items = read("Attacks/items.txt");
	}

	public String attack(String user) {
		String actionStr = actions.get(random.nextInt(actions.size()));
		String descriptionStr = descriptions.get(random.nextInt(descriptions.size()));

		Pattern usrPtrn = Pattern.compile("<user>");
		Pattern itmPtrn = Pattern.compile("<item>");

		Matcher usrMtch = usrPtrn.matcher(actionStr);
		actionStr = usrMtch.replaceAll(user);

		usrMtch = usrPtrn.matcher(descriptionStr);
		descriptionStr = usrMtch.replaceAll(user);

		Matcher itmMtch = itmPtrn.matcher(descriptionStr);
		if (itmMtch.find()) {
			String itemStr = items.get(random.nextInt(items.size()));
			Pattern namePtrn = Pattern.compile("<name>");
			Matcher m = namePtrn.matcher(itemStr);
			if (m.find()) {
				m.replaceAll(user);
			}
			descriptionStr = itmMtch.replaceAll(itemStr);
		}
		String sendMessage = actionStr + " " + user + " " + descriptionStr;
		return sendMessage;
	}

	private ArrayList<String> read(String filename) {
		ArrayList<String> s = new ArrayList<String>();
		try {
			BufferedReader br = null;
			File f = new File(filename);
			br = new BufferedReader(new FileReader(f));
			String line = null;
			while ((line = br.readLine()) != null) {
				s.add(line);
			}
		} catch (IOException ex) {
		}
		return s;
	}
}
